import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Binary3 extends JFrame implements ActionListener {
    private JComboBox<String>[] opCombo = new JComboBox[2];
    private JComboBox<String> OpCombo1;
    private Button eql;
    private TextField[] txt = new TextField[6];
    private TextField ans,ans1;
    @Override
    public void actionPerformed(ActionEvent e) {
        try {// 获取输入数值和运算符
            Integer num1 = Integer.parseInt(txt[0].getText());
            Integer num2 = Integer.parseInt(txt[2].getText());
            Integer num3 = Integer.parseInt(txt[4].getText());
            int result = mySwitch(opCombo[0],num1,num2);
            for (int i = 0; i < 4; i++) {
                result = mySwitch(opCombo[1],result,num3);
                txt[4].setText("");
                int ret = which();
                String num11 = MyInteger.toString(num1,ret);
                this.txt[1].setText(num11);
                String num22 = MyInteger.toString(num2,ret);
                this.txt[3].setText(num22);
                String num33 = MyInteger.toString(num3,ret);
                this.txt[5].setText(num33);
                this.ans.setText(Integer.toString(result));
                this.ans1.setText(MyInteger.toString(result,ret));
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "Invalid input", "Error", JOptionPane.ERROR_MESSAGE);
        } catch (ArithmeticException ex) {
            JOptionPane.showMessageDialog(this, "Divide by zero", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    public int which(){
        switch (OpCombo1.getSelectedIndex()){
            case 0: return 2;
            case 1: return 4;
            case 2: return 8;
            case 3: return 16;
        }
        return -1;
    }
    public int mySwitch(JComboBox<String> op, int num1, int num2){
        int result = 0;
        switch (op.getSelectedIndex()){
            case 0:  // 位与
                result = num1 & num2;
                break;
            case 1:  // 位或
                result = num1 | num2;
                break;
            case 2:  // 位异或
                result = num1 ^ num2;
                break;
            default:
                return num1;
        }
        return result;
    }
    public Binary3(){
        super("二进制运算");
        //关闭窗口
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //表格长宽
        this.setSize(600,200);
        //表格的位置
        this.setLocationRelativeTo(null);
        //表格框架
        this.setLayout(null);
        //进制选择
        OpCombo1 = new JComboBox<>(new String[]{"2", "4", "8","16"});
        OpCombo1.setBounds(220,0,40,20);
        this.add(OpCombo1);
        //第一个文本行
        this.txt[0] = new TextField("0",10);
        txt[0].setBounds(50,20,150,25);
        this.add(txt[0]);
        this.txt[1] = new TextField("",10);
        txt[1].setBounds(220,20,250,25);
        this.add(txt[1]);
        // 创建单选按钮显示并选择运算符
        opCombo[0] = new JComboBox<>(new String[]{"&", "|", "^"});
        opCombo[0].setBounds(0,46,40,25);
        this.add(opCombo[0]);
        //第二个文本行
        this.txt[2] = new TextField("0",10);
        txt[2].setBounds(50,46,150,25);
        this.add(txt[2]);

        this.txt[3] = new TextField("",10);
        txt[3].setBounds(220,46,250,25);
        this.add(txt[3]);

        opCombo[1] = new JComboBox<>(new String[]{"&","|","^","无"});
        opCombo[1].setBounds(0,72,40,25);
        this.add(opCombo[1]);

        this.txt[4] = new TextField("0",10);
        txt[4].setBounds(50,72,150,25);
        this.add(txt[4]);

        this.txt[5] = new TextField("",10);
        txt[5].setBounds(220,72,250,25);
        this.add(txt[5]);
        //"="按钮
        this.eql = new Button("=");
        eql.setBounds(0,100,40,25);
        this.add(this.eql);

        this.eql.addActionListener(this);
        this.ans = new TextField(10);
        ans.setBounds(50,100,150,25);
        this.add(ans);

        this.ans1 = new TextField(10);
        ans1.setBounds(220,100,250,25);
        this.add(ans1);

        this.setVisible(true);//可视化
    }
    public static void main(String[] args) {
        new Binary3();
    }
}
